package com.zxc.controller.equipment;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxc.model.Equipment.Equipment;
import com.zxc.model.Equipment.EquipmentApply;
import com.zxc.model.Page;
import com.zxc.model.Pesticide;
import com.zxc.service.ParkService;
import com.zxc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@CrossOrigin
@RestController
@RequestMapping("/equipment")
public class EquipmentController {
    @Autowired
    private ParkService parkService;



    @Autowired
    private UserService userService;

    @RequestMapping("/getEquipmentListByParkId")
    //通过前端传来的园区主键对设备表进行打印
    public ResponseEntity<?> getequipmentbyparkid(@RequestParam(value = "parkId") String parkId) {
        //调用parkService中的printEquipment方法进行设备信息的打印
        return ResponseEntity.ok(parkService.printEquipment(parkId));
    }

    @RequestMapping("/getEquipmentListByAccount")
    //通过前端传来的园区主键对设备表进行打印
    public ResponseEntity<?> getequipmentbyaccount(@RequestBody Equipment vo) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page =vo.getPageList().get(0);
        if("root".equals(page.getAccount())){
            PageHelper.startPage(page.getOffset(), page.getLimit());
            return ResponseEntity.ok(new PageInfo<Equipment>(parkService.selectEquipmentByAccount(vo)));
        }else{
            String id =userService.getUser(page.getAccount()).getId();
            vo.setUserId(id);
            PageHelper.startPage(page.getOffset(), page.getLimit());
            return ResponseEntity.ok(new PageInfo<Equipment>(parkService.selectEquipmentByUserAccount(vo)));
        }

    }

    @RequestMapping("/getEquipmentState")
    public ResponseEntity<?> getEquipmentState(@RequestBody EquipmentApply vo) {
        Page page =vo.getPageList().get(0);
        PageHelper.startPage(page.getOffset(), page.getLimit());
        return ResponseEntity.ok(new PageInfo<EquipmentApply>(parkService.selectEquipmentState(vo)));
    }
//    对设备报修单进行处理
    @RequestMapping("/setEquipmentState")
    public ResponseEntity<?> setEquipmentState(@RequestBody EquipmentApply vo) {
        return ResponseEntity.ok(parkService.setEquipmentState(vo));
    }


    @RequestMapping("/updateRowState")
    public ResponseEntity<?> updateRowState(@RequestBody Equipment vo) {
        Map<String, Object> response = new HashMap<String, Object>();
        parkService.updateRowState(vo);
        return ResponseEntity.ok(response);
    }


    @RequestMapping("/insertEquipmentApply")
    public ResponseEntity<?> insertEquipmentApply(@RequestBody EquipmentApply vo) {
        Map<String, Object> response = new HashMap<String, Object>();
        parkService.setEquipmentApply(vo);
        return ResponseEntity.ok(response);
    }


    @RequestMapping("/deleteEquipment")
    public ResponseEntity<?> deleteEquipment(@RequestBody Equipment vo) {
        Map<String, Object> response = new HashMap<String, Object>();
        parkService.deleteEquipmentById(vo);
        return ResponseEntity.ok(response);
    }

    @RequestMapping("/getEquipmentWarehouse")
    //通过前端传来的数据获取设备仓库信息，方法会进行判断是普通用户或者是管理员，分别进行不同表的查询
    public ResponseEntity<?> getEquipmentWarehouse(@RequestBody Equipment vo) {
        Map<String, Object> response = new HashMap<String, Object>();
        Page page =vo.getPageList().get(0);
        if("root".equals(page.getAccount())){
            response.put("RootEquipmentList",new PageInfo<Equipment>(parkService.getEquipmentWarehouseRoot(vo)));
        }else{
            response.put("UserEquipmentList",new PageInfo<Equipment>(parkService.getEquipmentWarehouseUser(vo)));
        }
        return ResponseEntity.ok(response);
    }


}
